GPX: Count size of entity replacements instead of divining it.
authorrobertl <robertl@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Thu, 2 Jan 2003 04:07:24 +0000 (04:07 +0000)
committerrobertl <robertl@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Thu, 2 Jan 2003 04:07:24 +0000 (04:07 +0000)
Magproto: More thoroughly close Windows handles.
From Mottram.

gpsbabel/gpx.c
gpsbabel/magproto.c

index 520f3c2eaa49ce339276a5284fcfa4870a87c91a..b73c84302d7a694dd1941f125065db4799b3bf8c 100644 (file)
@@ -58,8 +58,9 @@ static FILE *ofd;
 static
 char * gpx_entitize(const char * str) 
 {
-       int elen;
+       int elen, ecount;
        const char ** ep;
+       const char * cp;
        char * p, * tmp, * xstr;
        const char * stdentities[] = {
        "&",    "&amp;",
@@ -69,12 +70,28 @@ char * gpx_entitize(const char * str)
        "\"",   "&quot;",
        NULL,   NULL 
        };
+       ep = stdentities;
+       elen = ecount = 0;
 
-       /* enough space for the whole string at max entity size */
-       /* i.e. "'" == &quot;&apos;&quot; */
-       tmp = xcalloc(((strlen(str) * 6) + 1), 1);
+       /* figure # of entity replacements and additional size. */
+       while (*ep) {
+               cp = str;
+               while ((cp = strstr(cp, *ep)) != NULL) {
+                       elen += strlen(*(ep + 1)) - strlen(*ep);
+                       ecount++;
+                       cp += strlen(*ep);
+               }
+               ep += 2;
+       }
+
+       /* enough space for the whole string plus entity replacements, if any */
+       tmp = xcalloc((strlen(str) + elen + 1), 1);
        strcpy(tmp, str);
 
+       /* no entity replacements */
+       if (ecount == 0)
+               return (tmp);
+
        ep = stdentities;
 
        while (*ep) {
@@ -82,10 +99,10 @@ char * gpx_entitize(const char * str)
                while ((p = strstr(p, *ep)) != NULL) {
                        elen = strlen(*(ep + 1));
 
-                       xstr = xstrdup(p + 1);
+                       xstr = xstrdup(p + strlen(*ep));
 
+                       strcpy(p, *(ep + 1));
                        strcpy(p + elen, xstr);
-                       memcpy(p, *(ep + 1), elen);
 
                        free(xstr);
 
index f4dc054e5743047645ac01e61c98e4d76be6f929..a8cccb08a69920ff921db5590ce919d91d86f019 100644 (file)
@@ -430,7 +430,9 @@ mkspeed(bitrate)
        }
 }
 
-HANDLE comport;
+HANDLE comport = NULL;
+
+#define xCloseHandle(a) if (a) { CloseHandle(a); } a = NULL;
 
 static
 int
@@ -439,7 +441,11 @@ terminit(const char *portname)
        DCB tio;        
        COMMTIMEOUTS timeout;
 
-       comport = CreateFile(portname, GENERIC_READ|GENERIC_WRITE, 0, NULL, 
+        is_file = 0;
+
+       xCloseHandle(comport);
+
+       comport = CreateFile(portname, GENERIC_READ|GENERIC_WRITE, 0, NULL,
                          OPEN_EXISTING, 0, NULL);
 
        if (comport == INVALID_HANDLE_VALUE) {
@@ -467,7 +473,7 @@ terminit(const char *portname)
        tio.StopBits = ONESTOPBIT;
 
        if (!SetCommState (comport, &tio)) {
-               CloseHandle(comport);
+               xCloseHandle(comport);
 
                /*
                 *  Probably not a com port.   Try it as a file.
@@ -489,7 +495,7 @@ terminit(const char *portname)
        timeout.WriteTotalTimeoutMultiplier = 10;
        timeout.WriteTotalTimeoutConstant = 1000;
        if (!SetCommTimeouts (comport, &timeout)) {
-               CloseHandle (comport);
+               xCloseHandle (comport);
                fatal(MYNAME ": set timeouts\n");
        }
        return 1;
@@ -535,7 +541,7 @@ static
 void
 termdeinit()
 {
-       CloseHandle(comport);
+        xCloseHandle(comport);
 }
 
 #else
@@ -693,6 +699,11 @@ mag_wr_init(const char *portname, const char *args)
                if (magfile_out) {
                        fclose(magfile_out);
                }
+#if __WIN32__
+               if (comport) {
+                       xCloseHandle(comport);
+               }
+#endif
                mag_rd_init(portname, args);
        }
 }